<?php
class CChosen extends Chosen{
	public $options;
	
	/**
	 * 
	 * @param type $config
	 * @return type
	 * 
	 * fungsi ini menggantikan fungsi buildConfig pada Chosen::buildConfig
	 */
	private static function buildMyConfig($config)
    {
		if (isset($config['htmlOptions']['options'])) {
            $defaults = $config['htmlOptions']['options'];
            unset($config['htmlOptions']['options']);
            $config = array_merge($defaults, $config);
        }
        return $config;
    }
	
	/**
	 * Ke empat fungsi di bawah di override agar menggunakan fungsi buildMyConfig
	 */
	
	
	/** Single item select */
    public static function dropDownList($name, $select, $data, $htmlOptions = array())
    {
        return Yii::app()->getController()->widget(__CLASS__, self::buildMyConfig(array(
            'name' => $name,
            'value' => $select,
            'data' => $data,
            'htmlOptions' => $htmlOptions,
        )), true);
    }

    public static function activeDropDownList($model, $attribute, $data, $htmlOptions = array())
    {
        return Yii::app()->getController()->widget(__CLASS__, self::buildMyConfig(array(
            'model' => $model,
            'attribute' => $attribute,
            'data' => $data,
            'htmlOptions' => $htmlOptions,
        )), true);
    }

    /** Multiple items select */
    public static function multiSelect($name, $select, $data, $htmlOptions = array())
    {
        return Yii::app()->getController()->widget(__CLASS__, self::buildMyConfig(array(
            'name' => $name,
            'value' => $select,
            'data' => $data,
            'htmlOptions' => $htmlOptions,
            'multiple' => true,
        )), true);
    }

    public static function activeMultiSelect($model, $attribute, $data, $htmlOptions = array(),$options=array())
    {
        return Yii::app()->getController()->widget(__CLASS__, self::buildMyConfig(array(
            'model' => $model,
            'attribute' => $attribute,
            'data' => $data,
            'htmlOptions' => $htmlOptions,
            'multiple' => true,
			'options'=>$options,
        )), true);
    }
	
	/** Render widget html and register client scripts */
    public function run()
    {
        list($name, $id) = $this->resolveNameID();
        if (isset($this->htmlOptions['id']))
            $id = $this->htmlOptions['id'];
        else
            $this->htmlOptions['id'] = $id;
        if (isset($this->htmlOptions['name']))
            $name = $this->htmlOptions['name'];

        if ($this->multiple && substr($name, -2) !== '[]')
            $name .= '[]';

        if ($this->multiple && $this->sendEmpty) {
            echo CHtml::hiddenField(substr($name, 0, -2), '', array('id' => false));
        }
        if (isset($this->model)) {
			if(isset($this->options) && !empty($this->options)){
				echo CHtml::dropDownList($name, $this->options, $this->data, $this->htmlOptions);
			}else{
				echo CHtml::dropDownList($name, CHtml::resolveValue($this->model, $this->attribute), $this->data, $this->htmlOptions);
			}
        } else {
            echo CHtml::dropDownList($name, $this->value, $this->data, $this->htmlOptions);
        }
        $this->registerScripts($id);
    }
}
